package top.cross.webapp.common.webview;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import top.cross.webapp.common.R;
import top.cross.webapp.common.base.BaseStatusBarActivity;
import top.crossrun.webview.bridge.core.utils.BridgeHelper;
import top.crossrun.webview.bridge.core.utils.BridgeUtil;
import top.crossrun.webview.bridge.webview.x5.X5JSEval;
import top.crossrun.webview.bridge.webview.x5.X5WebViewChromeClient;
import top.crossrun.webview.bridge.webview.x5.X5WebViewHelper;

public class X5WebActivity extends BaseStatusBarActivity {

    private BridgeHelper bridgeHelper;

    private WebView webView;

    private X5WebViewChromeClient webViewChromeClient;

    String url = "https://www.qq.com";

    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        bridgeHelper = new BridgeHelper(this, new X5JSEval(webView));
        webViewChromeClient = new X5WebViewChromeClient(this, 1, (FrameLayout) findViewById(R.id.fullscreen), progressBar);
        X5WebViewHelper.initWebView(webView, webViewChromeClient);

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                if (bridgeHelper.shouldOverrideUrlLoading(BridgeUtil.decode(s), true)) {
                    return true;
                }
                if (!s.startsWith("http")) {
                    Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(s));
                    in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    if (BridgeUtil.isIntentAvailable(X5WebActivity.this, in)) {
                        startActivity(in);
                    } else {
                        Toast.makeText(X5WebActivity.this, "未安装匹配应用，无法打开", Toast.LENGTH_LONG).show();
                    }
                    return true;
                } else {
                    webView.loadUrl(s);
                }
                return true;
            }
        });
        webView.loadUrl(url);
    }

    @Override
    public int getLayoutRes() {
        return R.layout.activity_x5_web;
    }

    @Override
    public void initTitleLayout() {

    }

    @Override
    public void initIntentData() {

    }

    @Override
    public void initView() {
        webView = findViewById(R.id.x5webview);
        progressBar = findViewById(R.id.progress);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        webViewChromeClient.onActivityResult(requestCode, resultCode, data);
        bridgeHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        bridgeHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}
